//
//  YYEChapterTableViewCell.m
//  YuYingEDU
//
//  Created by 胡波 on 2018/1/11.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYEChapterTableViewCell.h"
#import "YYEExampleViewController.h"

@interface YYEChapterTableViewCell ()



@end

@implementation YYEChapterTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.contentView.backgroundColor = GeneralBackColor;
        
        self.backIV = [[UIImageView alloc]init];
        self.backIV.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.backIV];
        
        self.leftLine = [[UIView alloc]init];
        self.leftLine.backgroundColor = GeneralBlueColor;
        [self.contentView addSubview:self.leftLine];
        
        self.btn = [[UIButton alloc] init];
        [self.btn setImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];

        [self.contentView addSubview:self.btn];

        
        self.nameLabel = [[UILabel alloc]init];
        self.nameLabel.textColor = [UIColor blackColor];
        self.nameLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.nameLabel];
        
        self.timeLabel = [[UILabel alloc]init];
//        self.timeLabel.textColor = GeneralBlueColor;
        self.timeLabel.font = [UIFont systemFontOfSize:12];
        self.timeLabel.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:self.timeLabel];
        
        self.examIV = [[UIImageView alloc]init];
        self.examIV.image = [UIImage imageNamed:@"exam"];// noplay
        [self.contentView addSubview:self.examIV];
        self.examIV.userInteractionEnabled = YES;
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(examAction)];
        [self.examIV addGestureRecognizer:tap];
        
        self.nameLabel.text = @"3.1计算机系统1";
        self.timeLabel.text = @"20分钟";
        
    }
    return self;
    
}

- (void)examAction
{
    YYEExampleViewController *vc = [[YYEExampleViewController alloc]init];
    vc.hid = _model.hid;
    vc.mid = _model.id;
    vc.type = _model.type;
    //        vc.process = model.process;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.backIV makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsMake(0, 8, 0, 8));
    }];
    
    [self.leftLine makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backIV.left).offset(28);
        make.top.bottom.equalTo(self.backIV);
        make.width.equalTo(1);
    }];
    
    [self.btn makeConstraints:^(MASConstraintMaker *make) {
        //        make.right.equalTo(self.timeLabel.left).offset(-20);
        make.left.equalTo(self.leftLine.right).offset(26);
        make.centerY.equalTo(self.backIV.centerY);
        make.size.equalTo(CGSizeMake(15, 15));
    }];
    
    [self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftLine.right).offset(50);
        make.centerY.equalTo(self.backIV.centerY);
    }];
    
    [self.timeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.backIV.right).offset(-13);
        make.centerY.equalTo(self.backIV.centerY);
    }];
    
    [self.examIV makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self.timeLabel.left).offset(-20);
        make.right.equalTo(self.contentView.right).offset(-90);
        make.centerY.equalTo(self.contentView.centerY);
        make.size.equalTo(CGSizeMake(21, 21));
    }];
    
}

- (void)setModel:(YYELittleChapterModel *)model {
    _model = model;
    
    NSInteger videoT = model.videoTime.integerValue;
    
    self.nameLabel.text = model.name;
    
    
    if (videoT < 60) {
        self.timeLabel.text = @"小于1分钟";
        
    }
    else {
        self.timeLabel.text = [NSString stringWithFormat:@"%@分钟",@(videoT / 60).stringValue];
        
    }
    
//    if (model.currutSelected) {
//        self.nameLabel.textColor = [UIColor purpleColor];
//
//    }
//    else {
//        self.nameLabel.textColor = [UIColor blackColor];
//
//    }
    
    // 有无试卷或习题
    if (model.hid.integerValue > 0) {
        self.examIV.hidden = NO;
        
    }
    else {
        self.examIV.hidden = YES;

    }
    
    // 最新进度
    if (model.selected) {
        self.btn.hidden = NO;
        
    }
    else {
        self.btn.hidden = YES;
        
    }
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
